package list;

/**
 * @ClassName RemoveListNodeElement
 * @Description TODO
 * @Author lenovo
 * @Date 2023-02-14 20:29
 * @Version 1.0
 * @Comment Magic. Do not touch.
 * If this comment is removed. the program will blow up
 */
public class RemoveListNodeElement {

    /**
     * 203. 移除链表元素
     * 给你一个链表的头节点 head 和一个整数 val ，请你删除链表中所有满足 Node.val == val 的节点，并返回 新的头节点 。
     * <p>
     * 示例 1：
     * <p>
     * 输入：head = [1,2,6,3,4,5,6], val = 6
     * 输出：[1,2,3,4,5]
     */

    public static ListNode removeElements(ListNode head, int val) {

        ListNode listNode = new ListNode();
        listNode.next = head;
        ListNode cur = listNode;

        while (cur != null && cur.next != null) {
            if (cur.next.val == val) {
                cur.next = cur.next.next;
            } else {
                cur = cur.next;
            }
        }
        return listNode.next;
    }

    public static void main(String[] args) {
        ListNode head1 = new ListNode(1);
        ListNode head2 = new ListNode(2);
        ListNode head6 = new ListNode(6);
        ListNode head3 = new ListNode(3);
        ListNode head4 = new ListNode(4);
        ListNode head = new ListNode(6);
        head1.next = head2;
        head2.next = head6;
        head6.next = head3;
        head3.next = head4;
        head4.next = head;
        ListNode listNode = removeElements(head1, 6);
        System.out.println(listNode.toString());
    }
}